import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'index.dart';
import '../../../common/models/index.dart';

class CaigouAssessmentPage extends GetView<CaigouAssessmentController> {
  const CaigouAssessmentPage({super.key});

  // 主视图
  Widget _buildView() {
    return Obx(() {
      // 显示加载状态
      if (controller.isLoading.value) {
        return Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              CircularProgressIndicator(
                valueColor: AlwaysStoppedAnimation<Color>(Colors.blue.shade400),
                strokeWidth: 2.5,
              ),
              const SizedBox(height: 12),
              Text(
                '正在加载评标项目...',
                style: TextStyle(
                  fontSize: 13,
                  color: Colors.grey[600],
                  fontWeight: FontWeight.w500,
                ),
              ),
            ],
          ),
        );
      }

      // 显示错误状态
      if (controller.errorMessage.value.isNotEmpty) {
        return Center(
          child: Padding(
            padding: const EdgeInsets.all(24),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                  padding: const EdgeInsets.all(12),
                  decoration: BoxDecoration(
                    color: Colors.red.shade50,
                    shape: BoxShape.circle,
                  ),
                  child: Icon(
                    Icons.error_outline_rounded,
                    size: 40,
                    color: Colors.red.shade400,
                  ),
                ),
                const SizedBox(height: 16),
                Text(
                  '加载失败',
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                    color: Colors.grey[800],
                  ),
                ),
                const SizedBox(height: 6),
                Text(
                  controller.errorMessage.value,
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: 12,
                    color: Colors.grey[600],
                  ),
                ),
                const SizedBox(height: 16),
                ElevatedButton(
                  onPressed: controller.refreshProjectList,
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.white,
                    foregroundColor: Colors.blue.shade600,
                    elevation: 0,
                    padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 10),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(20),
                      side: BorderSide(color: Colors.blue.shade200, width: 1.5),
                    ),
                  ),
                  child: const Text(
                    '重试',
                    style: TextStyle(fontSize: 13, fontWeight: FontWeight.w600),
                  ),
                ),
              ],
            ),
          ),
        );
      }

      // 显示空数据状态
      if (controller.projectList.isEmpty) {
        return Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Container(
                padding: const EdgeInsets.all(16),
                decoration: BoxDecoration(
                  color: Colors.grey.shade100,
                  shape: BoxShape.circle,
                ),
                child: Icon(
                  Icons.assignment_outlined,
                  size: 48,
                  color: Colors.grey.shade400,
                ),
              ),
              const SizedBox(height: 16),
              Text(
                '暂无评标项目',
                style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.bold,
                  color: Colors.grey[700],
                ),
              ),
              const SizedBox(height: 6),
              Text(
                '当前没有需要评审的项目',
                style: TextStyle(
                  fontSize: 12,
                  color: Colors.grey[500],
                ),
              ),
              const SizedBox(height: 16),
              ElevatedButton(
                onPressed: controller.refreshProjectList,
                style: ElevatedButton.styleFrom(
                  backgroundColor: Colors.white,
                  foregroundColor: Colors.blue.shade600,
                  elevation: 0,
                  padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 10),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(20),
                    side: BorderSide(color: Colors.blue.shade200, width: 1.5),
                  ),
                ),
                child: const Text(
                  '刷新',
                  style: TextStyle(fontSize: 13, fontWeight: FontWeight.w600),
                ),
              ),
            ],
          ),
        );
      }

      // 显示项目列表
      return RefreshIndicator(
        onRefresh: controller.refreshProjectList,
        color: Colors.blue.shade400,
        backgroundColor: Colors.white,
        child: ListView.builder(
          padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
          itemCount: controller.projectList.length,
          itemBuilder: (context, index) {
            final project = controller.projectList[index];
            return _buildProjectCard(project);
          },
        ),
      );
    });
  }

  // 构建项目卡片
  Widget _buildProjectCard(project) {
    return Container(
      margin: const EdgeInsets.only(bottom: 10),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.03),
            blurRadius: 8,
            offset: const Offset(0, 1),
          ),
        ],
        border: Border.all(
          color: Colors.grey.shade100,
          width: 1,
        ),
      ),
      child: Padding(
        padding: const EdgeInsets.all(12),
        child: Material(
          color: Colors.transparent,
          child: Padding(
            padding: EdgeInsets.zero,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // 项目标题和状态
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      padding: const EdgeInsets.all(7),
                      decoration: BoxDecoration(
                        color: Colors.blue.shade50,
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child: Icon(
                        Icons.gavel_rounded,
                        color: Colors.blue.shade600,
                        size: 18,
                      ),
                    ),
                    const SizedBox(width: 10),
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            project.projectName ?? '未命名项目',
                            style: TextStyle(
                              fontSize: 15,
                              fontWeight: FontWeight.bold,
                              color: Colors.grey[900],
                              height: 1.2,
                            ),
                            maxLines: 2,
                            overflow: TextOverflow.ellipsis,
                          ),
                          const SizedBox(height: 4),
                          Container(
                            padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 3),
                            decoration: BoxDecoration(
                              color: _getStatusColor(project.status).withOpacity(0.1),
                              borderRadius: BorderRadius.circular(12),
                              border: Border.all(
                                color: _getStatusColor(project.status).withOpacity(0.3),
                                width: 0.8,
                              ),
                            ),
                            child: Text(
                              _getStatusText(project.status),
                              style: TextStyle(
                                color: _getStatusColor(project.status),
                                fontSize: 11,
                                fontWeight: FontWeight.w600,
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
                
                const SizedBox(height: 10),
                
                // 分隔线
                Divider(color: Colors.grey.shade100, height: 1, thickness: 1),
                
                const SizedBox(height: 10),
                
                // 项目信息
                _buildInfoRow(
                  icon: Icons.tag_rounded,
                  iconColor: Colors.purple.shade400,
                  iconBgColor: Colors.purple.shade50,
                  label: '编号',
                  value: project.projectNo ?? '--',
                ),
                
                const SizedBox(height: 8),
                
                _buildInfoRow(
                  icon: Icons.business_rounded,
                  iconColor: Colors.orange.shade400,
                  iconBgColor: Colors.orange.shade50,
                  label: '申报部门',
                  value: project.declareDepartment ?? '--',
                ),
                
                const SizedBox(height: 8),
                
                _buildInfoRow(
                  icon: Icons.person_rounded,
                  iconColor: Colors.teal.shade400,
                  iconBgColor: Colors.teal.shade50,
                  label: '联系人',
                  value: project.projectContact ?? '--',
                ),
                
                const SizedBox(height: 8),
                
                _buildInfoRow(
                  icon: Icons.account_balance_wallet_rounded,
                  iconColor: Colors.green.shade500,
                  iconBgColor: Colors.green.shade50,
                  label: '预算',
                  value: project.budgetAmount != null && project.budgetAmount.toString() != 'null'
                      ? '${project.budgetAmount}万元' 
                      : '--',
                  isHighlight: true,
                ),
                
                const SizedBox(height: 12),
                
                // 操作按钮
                SizedBox(
                  width: double.infinity,
                  child: ElevatedButton(
                    onPressed: () => controller.onProjectTap(project),
                    style: ElevatedButton.styleFrom(
                      backgroundColor: Colors.blue.shade600,
                      foregroundColor: Colors.white,
                      elevation: 0,
                      padding: const EdgeInsets.symmetric(vertical: 10),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10),
                      ),
                      shadowColor: Colors.blue.withOpacity(0.3),
                    ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        const Icon(Icons.play_arrow_rounded, size: 18),
                        const SizedBox(width: 4),
                        const Text(
                          '开始评标',
                          style: TextStyle(
                            fontSize: 14,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
  
  // 构建信息行
  Widget _buildInfoRow({
    required IconData icon,
    required Color iconColor,
    required Color iconBgColor,
    required String label,
    required String value,
    bool isHighlight = false,
  }) {
    return Row(
      children: [
        Container(
          padding: const EdgeInsets.all(5),
          decoration: BoxDecoration(
            color: iconBgColor,
            borderRadius: BorderRadius.circular(6),
          ),
          child: Icon(icon, color: iconColor, size: 14),
        ),
        const SizedBox(width: 8),
        Text(
          '$label: ',
          style: TextStyle(
            fontSize: 12,
            color: Colors.grey[600],
            fontWeight: FontWeight.w500,
          ),
        ),
        Expanded(
          child: Text(
            value,
            style: TextStyle(
              fontSize: 12,
              color: isHighlight ? Colors.green.shade700 : Colors.grey[800],
              fontWeight: isHighlight ? FontWeight.w700 : FontWeight.w600,
            ),
            overflow: TextOverflow.ellipsis,
          ),
        ),
      ],
    );
  }

  // 获取状态颜色（使用ProjectModel的状态管理）
  Color _getStatusColor(int? status) {
    return ProjectModel.getStatusColorByValue(status);
  }

  // 获取状态文本（使用ProjectModel的状态管理）
  String _getStatusText(int? status) {
    return ProjectModel.getStatusTextByValue(status);
  }

  @override
  Widget build(BuildContext context) {
    // 从路由参数获取动态标题
    final arguments = Get.arguments as Map<String, dynamic>? ?? {};
    final pageTitle = arguments['pageTitle'] as String? ?? '项目采购评审';
    
    return GetBuilder<CaigouAssessmentController>(
      init: CaigouAssessmentController(),
      id: "caigou_assessment",
      builder: (_) {
        return Scaffold(
          backgroundColor: Colors.grey.shade50,
          appBar: AppBar(
            elevation: 0,
            backgroundColor: Colors.white,
            surfaceTintColor: Colors.white,
            toolbarHeight: 50,
            leading: IconButton(
              onPressed: () => Navigator.pop(context),
              padding: const EdgeInsets.all(8),
              icon: Icon(
                Icons.arrow_back_ios_new_rounded,
                color: Colors.grey[800],
                size: 18,
              ),
            ),
            title: Text(
              pageTitle,
              style: TextStyle(
                color: Colors.grey[900],
                fontSize: 16,
                fontWeight: FontWeight.bold,
              ),
            ),
            actions: [
              Container(
                margin: const EdgeInsets.only(right: 8),
                child: IconButton(
                  padding: EdgeInsets.zero,
                  icon: Container(
                    padding: const EdgeInsets.all(7),
                    decoration: BoxDecoration(
                      color: Colors.blue.shade50,
                      borderRadius: BorderRadius.circular(8),
                    ),
                    child: Icon(
                      Icons.refresh_rounded,
                      color: Colors.blue.shade600,
                      size: 18,
                    ),
                  ),
                  onPressed: controller.refreshProjectList,
                ),
              ),
            ],
            bottom: PreferredSize(
              preferredSize: const Size.fromHeight(1),
              child: Container(
                height: 1,
                color: Colors.grey.shade100,
              ),
            ),
          ),
          body: SafeArea(
            child: _buildView(),
          ),
        );
      },
    );
  }
}
